home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / LEXICON.PY < prev    next >
Encoding:
Python Source  |  2000-06-27  |  9.5 KB  |  242 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64.  
  65. import string, regex, ts_regex
  66. import regsub
  67.  
  68.  
  69.  
  70. __doc__=""" Module breaks out Zope specific methods and behavior.  In
  71. addition, provides the Lexicon class which defines a word to integer
  72. mapping.
  73.  
  74. """
  75.  
  76. from Splitter import Splitter
  77. from Persistence import Persistent
  78. from Acquisition import Implicit
  79. import OIBTree, BTree
  80. OIBTree=OIBTree.BTree
  81. OOBTree=BTree.BTree
  82. import re
  83.  
  84.  
  85. class Lexicon(Persistent, Implicit):
  86.     """Maps words to word ids and then some
  87.  
  88.     The Lexicon object is an attempt to abstract vocabularies out of
  89.     Text indexes.  This abstraction is not totally cooked yet, this
  90.     module still includes the parser for the 'Text Index Query
  91.     Language' and a few other hacks.
  92.  
  93.     """
  94.  
  95.     # default for older objects
  96.     stop_syn={}
  97.  
  98.     def __init__(self, stop_syn=None):
  99.         self._lexicon = OIBTree()
  100.         self.counter = 0
  101.         if stop_syn is None:
  102.             self.stop_syn = {}
  103.         else:
  104.             self.stop_syn = stop_syn
  105.  
  106.                 
  107.     def set_stop_syn(self, stop_syn):
  108.         """ pass in a mapping of stopwords and synonyms.  Format is:
  109.  
  110.         {'word' : [syn1, syn2, ..., synx]}
  111.  
  112.         Vocabularies do not necesarily need to implement this if their
  113.         splitters do not support stemming or stoping.
  114.  
  115.         """
  116.         self.stop_syn = stop_syn
  117.         
  118.  
  119.     def set(self, word):
  120.         """ return the word id of 'word' """
  121.  
  122.         if self._lexicon.has_key(word):
  123.             return self._lexicon[word]
  124.  
  125.         else:
  126.             if not hasattr(self, 'counter'):
  127.                 self.counter = 0
  128.             self._lexicon[intern(word)] = self.counter
  129.             self.counter = self.counter + 1
  130.             return self.counter - 1 
  131.  
  132.     def get(self, key, default=None):
  133.         """  """
  134.         return [self._lexicon.get(key, default)]
  135.  
  136.     def __getitem__(self, key):
  137.         return self.get(key)
  138.  
  139.     def __len__(self):
  140.         return len(self._lexicon)
  141.  
  142.     def Splitter(self, astring, words=None):
  143.         """ wrap the splitter """
  144.         if words is None:
  145.             words = self.stop_syn
  146.         return Splitter(astring, words)
  147.  
  148.     def grep(self, query):
  149.         """
  150.         regular expression search through the lexicon
  151.         he he.
  152.  
  153.         Do not use unless you know what your doing!!!
  154.         """
  155.         expr = re.compile(query)
  156.         hits = []
  157.         for x in self._lexicon.keys():
  158.             if expr.search(x):
  159.                 hits.append(x)
  160.         return hits
  161.  
  162.     def query_hook(self, q):
  163.         """ we don't want to modify the query cuz we're dumb """
  164.         return q
  165.         
  166.  
  167.  
  168.  
  169.  
  170. stop_words=(
  171.     'am', 'ii', 'iii', 'per', 'po', 're', 'a', 'about', 'above', 'across',
  172.     'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone',
  173.     'along', 'already', 'also', 'although', 'always', 'am', 'among',
  174.     'amongst', 'amoungst', 'amount', 'an', 'and', 'another', 'any',
  175.     'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'around',
  176.     'as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes',
  177.     'becoming', 'been', 'before', 'beforehand', 'behind', 'being',
  178.     'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both',
  179.     'bottom', 'but', 'by', 'can', 'cannot', 'cant', 'con', 'could',
  180.     'couldnt', 'cry', 'describe', 'detail', 'do', 'done', 'down', 'due',
  181.     'during', 'each', 'eg', 'eight', 'either', 'eleven', 'else',
  182.     'elsewhere', 'empty', 'enough', 'even', 'ever', 'every', 'everyone',
  183.     'everything', 'everywhere', 'except', 'few', 'fifteen', 'fifty',
  184.     'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly',
  185.     'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get',
  186.     'give', 'go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her',
  187.     'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers',
  188.     'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'i',
  189.     'ie', 'if', 'in', 'inc', 'indeed', 'interest', 'into', 'is', 'it',
  190.     'its', 'itself', 'keep', 'last', 'latter', 'latterly', 'least',
  191.     'less', 'made', 'many', 'may', 'me', 'meanwhile', 'might', 'mill',
  192.     'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must',
  193.     'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless',
  194.     'next', 'nine', 'no', 'nobody', 'none', 'noone', 'nor', 'not',
  195.     'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once',
  196.     'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our',
  197.     'ours', 'ourselves', 'out', 'over', 'own', 'per', 'perhaps',
  198.     'please', 'pre', 'put', 'rather', 're', 'same', 'see', 'seem',
  199.     'seemed', 'seeming', 'seems', 'serious', 'several', 'she', 'should',
  200.     'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some',
  201.     'somehow', 'someone', 'something', 'sometime', 'sometimes',
  202.     'somewhere', 'still', 'such', 'take', 'ten', 'than', 'that', 'the',
  203.     'their', 'them', 'themselves', 'then', 'thence', 'there',
  204.     'thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these',
  205.     'they', 'thick', 'thin', 'third', 'this', 'those', 'though', 'three',
  206.     'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too',
  207.     'toward', 'towards', 'twelve', 'twenty', 'two', 'un', 'under',
  208.     'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we', 'well',
  209.     'were', 'what', 'whatever', 'when', 'whence', 'whenever', 'where',
  210.     'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon',
  211.     'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever',
  212.     'whole', 'whom', 'whose', 'why', 'will', 'with', 'within', 'without',
  213.     'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves',
  214.     )
  215. stop_word_dict={}
  216. for word in stop_words: stop_word_dict[word]=None
  217.  
  218.  
  219.  
  220.  
  221.